package com.frxuexi.document.view

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.document.DocIntent
import com.frxuexi.document.DocViewModel
import com.frxuexi.document.entity.DocEntity
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.HttpType
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun DocPage(navHostController: NavHostController,vm: DocViewModel= hiltViewModel()) {
    var list = remember {
        SnapshotStateList<DocEntity>()
    }

    var index by remember {
        mutableStateOf(-1)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        if(it.type==HttpType.DELETE){
                            ToastUtils.showLong("删除成功")
//                            vm.sendIntent(DocIntent.getDocs(mapOf(Pair("userId",1),Pair("page",1))))
                            list.removeAt(index)
                        }else {
                            list.clear()
                            Log.i("===", it.data.toString())
                            list.addAll(it.data as Collection<DocEntity>)
                        }
                    }
                    is UiState.onError->{
                        Log.i("===",it.msg)
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(DocIntent.getDocs(mapOf(Pair("userId",1),Pair("page",1))))
    })


    Scaffold(floatingActionButton = {
        FloatingActionButton(onClick = {
            navHostController.navigate(RoutePath.LOCAL)
        }) {
            Icon(imageVector = Icons.Default.Add, contentDescription = "")
        }
    }) {
        Column(
            Modifier
                .padding(it)
                .padding(10.dp)) {
            BaseTitle(title = "文件", navHostController = navHostController)
            Spacer(modifier = Modifier.height(10.dp))
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp),content = {
                items(list){
                    DocItem(entity = it,vm){
//                        list-=it
                        index=list.indexOf(it)
                    }

                }
            })
        }
    }

}

@Composable
fun DocItem(entity: DocEntity,vm: DocViewModel,onClick: () -> Unit){
    var isShow by remember {
        mutableStateOf(false)
    }
    Row(Modifier.clickable {
        isShow=true
    }) {
        AsyncImage(model = entity.filePath, contentScale= ContentScale.Crop,contentDescription = "", modifier = Modifier
            .size(50.dp)
            .clip(
                RoundedCornerShape(20.dp)
            ))
        Spacer(modifier = Modifier.width(10.dp))
        Column(Modifier.height(50.dp), verticalArrangement = Arrangement.SpaceBetween) {
            Text(text = entity.fileName)
            val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(entity.fileTime)
            Text(text = time)
        }
    }

    if(isShow){
        AlertDialog(onDismissRequest = { isShow=false },
            confirmButton = { Text(text = "确认", Modifier.clickable {
                vm.sendIntent(DocIntent.deleteDoc(mapOf("FileId" to entity.fileId)))
                onClick()
                isShow=false
            }) },
            title = { Text(text = "删除提醒")},
            text = { Text(text = "确认删除${entity.fileName}吗？")})
    }
}